/*
 Copyright (C) 2009 QDSS.org
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.qdss.persist.engine;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;

import org.qdss.persist.LRTest;
import org.qdss.persist.Record;
import org.qdss.persist.dialect.FieldType;
import org.qdss.persist.engine.entry.ID;
import org.qdss.persist.engine.entry.StandardRecord;
import org.springframework.jdbc.datasource.DataSourceUtils;

/**
 * 
 * 
 * @author <a href="mailto:zhaofang123@gmail.com">FANGFANG ZHAO</a>
 * @since 0.1.0, Mar 23, 2009
 * @version $Id: Lob.java 52 2009-05-25 09:54:02Z zhaofang123@gmail.com $
 */
public class Lob extends LRTest {
	
	public void test() throws Exception {
		Connection connect = DataSourceUtils.getConnection(aPMFactory.getDataSource());
		PreparedStatement pstmt = connect.prepareStatement("insert into EveryType(`binary`, `primary`) values (?, ?)");
		
		File file = new File("E:/TDDOWNLOAD/LiveBOS(灵动业务架构平台)演示.rar");
		InputStream stream = new FileInputStream(file);
		
		Record record = new StandardRecord(aPMFactory.getMetadataFactory().getEntity("EveryType"), null);
		record.setObjectValue("binary", stream);
		
//		pstmt.setBinaryStream(1, stream, (int)file.length());
		FieldType.BINARY.getFieldEditor().set(pstmt, 1, record.getObjectValue("binary"));
		pstmt.setString(2, ID.newId(001).toLiteral());
		pstmt.executeUpdate();
		
		pstmt.close();
		connect.close();
	}

//	public static void main(String[] args) throws Exception {
//		Class.forName(Driver.class.getName());
//		
//		Connection connect = java.sql.DriverManager.getConnection(
//				"jdbc:mysql://127.0.0.1:3306/openoce000?useUnicode=true&amp;characterEncoding=UTF-8", "root", "root");
//		PreparedStatement pstmt = connect.prepareStatement("insert into EveryType(`binary`, `primary`) values (?, ?)");
//		
//		File file = new File("e:/Mart_1236844220178.xml.tmp");
//		InputStream stream = new FileInputStream(file);
//		pstmt.setBinaryStream(1, stream, file.length());
//		pstmt.setString(2, ID.newId(001).toLiteral());
//		pstmt.execute();
//		
//		pstmt.close();
//		connect.close();
//	}
}
